<?php
	require_once(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR ."core".DIRECTORY_SEPARATOR."functions.php");
	require_once("Database.php");

	class Register extends dbConn
	{
		private $firstName,$lastName,$gender,$contactNumber;
		private $email,$password;

		private $resetKey,$activationKey,$salt;

		private $accountType,$activated;

		public function __construct($firstName,$lastName,$email,$password,$gender,$contactNumber,$accountType=ACC_VOLUNTEER)
		{
			$this->firstName = $firstName;
			$this->lastName = $lastName;
			$this->email = $email;
			$this->password = $password;
			$this->gender = $gender;
			$this->contactNumber = $contactNumber;	
			$this->resetKey = substr(md5($email.$password),11);
			$this->activationKey = substr(md5($password.$email),11);
			$this->accountType = $accountType;
			$this->activated = 0;

			$rand = mt_rand(1,99);
			if($rand < 10)
			{
				$rand = "0".$rand;
			}
			$this->salt = $rand;
			$this->password = md5($this->password.$this->salt);

			//echo "$this->password<br/>$this->resetKey<br/>$this->activationkey<br/>";
		}


		/**
			@input : n/a
			@output : boolean
			@description : This function checks wheather the email is already present in the database and returns true if so.
		 */
			
		public function pass()
		{
			$qry = "SELECT * FROM person WHERE ".P_E."='$this->email'";
			$res = $this->query($qry);

			if($this->num_rows($res) != 0)
			{
				return false;
			}
			else
			{
				return true;
			}
		}


		/**
			@input : n/a
			@output : boolean
			@description : This function tries to insert user's details into the 'login' table and return boolean on its result
		 */
		public function finish()
		{
			$q = "INSERT INTO person (".P_FN.",".P_LN.",".P_CN.",".P_G.",".P_P.",".P_S.",".P_E.",".P_AT.",".P_ACT.",".P_RK.",".P_AK.") VALUES 
														('$this->firstName', '$this->lastName','$this->contactNumber','$this->gender','$this->password','$this->salt','$this->email','$this->accountType',$this->activated,'$this->resetKey','$this->activationKey')";
			$qry = $this->insert($q);

			if($qry)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		public function getId()
		{
			$qry = $this->query("SELECT personId FROM person WHERE email='$this->email' AND password='$this->password' LIMIT 1");
			$data = $this->fetch_array($qry);
			return $data['personId'];
		}

		/**
			@input n/a
			@output : string
			@description : This function will retrieve the activation key of the user
		*/
		public function getActivationKey()
		{
			return $this->activationKey;
		}
	}

?>